﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BusinessLayer;
using System.Data.SqlClient;

namespace Design
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                ShoppingCart objcart = new ShoppingCart();
                objcart.userid = System.Web.Security.Membership.GetUser().ProviderUserKey.ToString().ToUpper();

                 GridView1.DataSource = objcart.get_cart_items_for_a_user();
                 GridView1.DataBind(); 
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            if (row != null)
            {
                LinkButton LinkButton1 = (LinkButton)sender;

                // Get reference to the row that hold the button
                GridViewRow gvr = (GridViewRow)LinkButton1.NamingContainer;

                // Get row index from the row
                int rowIndex = gvr.RowIndex;
                string str = rowIndex.ToString();
                //string str = GridView1.DataKeys[row.RowIndex].Value.ToString();
                //RemoveData(str); //call the delete method

                
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            //handling gridview delete excetion
            e.ExceptionHandled = true;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string id = e.CommandArgument.ToString();
                string userid = System.Web.Security.Membership.GetUser().ProviderUserKey.ToString().ToUpper();

                ShoppingCart objcart = new ShoppingCart();

                objcart.equip_id = id;
                objcart.userid = userid;
                objcart.delete_item();

                GridView1.DataSource = objcart.get_cart_items_for_a_user();
                GridView1.DataBind();

                
            }
            catch
            {

            }
        }

     }
}